package cn.cs.controller;

import cn.cs.common.*;
import cn.cs.pojo.CsFactory;
import cn.cs.service.CsFactoryService;
import cn.cs.service.CsOrgService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping(value = "/factory")
public class FactoryController {

    @Resource
    private CsFactoryService csFactoryService;

    @Resource
    private CsOrgService csOrgService;


    /**
     * 分页查询 电器厂列表
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws Exception
     * TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getFactoryByPage")
    public Dto getFactoryByPage(Integer pageNumber, Integer pageSize, String factoryName, String factoryCode, String factoryPerson, String factoryPhone)throws Exception{
        Map<String, Object> param = new HashMap<>();
        param.put("factoryName", factoryName);
        param.put("factoryCode", factoryCode);
        param.put("factoryPerson", factoryPerson);
        param.put("factoryPhone", factoryPhone);
        return DtoUtil.returnSuccess("获取成功", csFactoryService.queryCsFactoryPageByMap(param, pageNumber, pageSize));
    }

    /**
     * 查询所有电器厂
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/p/getFactoryAll")
    public Dto getFactoryAll()throws Exception{
        Map<String, Object> param = new HashMap<>();
        return DtoUtil.returnSuccess("获取成功", csFactoryService.getCsFactoryListByMap(param));
    }

    /**
     * 新增/编辑 电器厂档案
     * @param csFactory
     * @return
     */
    @RequestMapping(value = "/csPlatform/updateFactory")
    public Dto updateFactory(@RequestBody CsFactory csFactory){
        try {
            String msg = "新增成功";
            if (EmptyUtils.isEmpty(csFactory.getId())) {
                csFactory.setId(IdWorker.getId());
                csFactory.setFactoryPassword(MD5Util.encode(csFactory.getFactoryPassword()));
                String code = String.format("%03d", csOrgService.getMaxCode("pjc_code"));
                csFactory.setFactoryCode(PingYinUtil.getFirstSpell(csFactory.getFactoryName()) + csFactory.getFactoryPhone().substring(7,11) + code);

                csFactory.setCreateDate(new Date());
                csFactoryService.addCsFactory(csFactory);
            } else {
                if (EmptyUtils.isNotEmpty(csFactory.getFactoryPassword())) {
                    csFactory.setFactoryPassword(MD5Util.encode(csFactory.getFactoryPassword()));
                }
                csFactory.setModifyDate(new Date());
                msg = "修改成功";
                csFactoryService.modifyCsFactory(csFactory);
            }
            return DtoUtil.returnSuccess(msg);
        }catch (DuplicateKeyException d){
            d.printStackTrace();
            return DtoUtil.returnFail("员工编号已存在，请更换后重试", "002");
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }


    /**
     * 删除 电器厂档案
     * @param ids
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/delFactory")
    public Dto delFactory(String ids)throws Exception{
        int i = 0;
        for(String id:ids.split(",")){
            i += csFactoryService.deleteCsFactoryById(id);
        }
        if(i>0){
            return DtoUtil.returnSuccess("操作成功", i);
        }else {
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }
}
